package com.andova.component.config;

import android.text.Html;
import android.view.View;
import android.widget.Spinner;
import android.widget.TextView;

import com.andova.toolkit.adapter.RecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018-08-21.
 *
 * @author kzaxil
 * @since 0.0.2
 */
public class ConfigAdapter extends RecyclerAdapter<ConfigBean> {
    static final String DEFAULT_SPINNER_STRING = "点击更改配置项值";

    @Override
    public Integer onCreateViewHolder() {
        return R.layout.item_component_config;
    }

    @Override
    public void execute(int position, View itemView, ConfigBean model) {
        TextView tvValue = itemView.findViewById(R.id.tv_value);
        tvValue.setText(Html.fromHtml(itemView.getResources().getString(R.string.format_value_html, model.node, model.value)));

        if (model.alternative == null) return;
        Spinner spinner = itemView.findViewById(R.id.sp_list);
        spinner.setAdapter(null);
        spinner.setOnItemSelectedListener(null);
        spinner.setVisibility(View.VISIBLE);
        SpinnerAdapter adapter = new SpinnerAdapter(model.node);
        spinner.setOnItemSelectedListener(adapter);
        List<Value> list = new ArrayList<>(model.alternative.size() + 1);
        list.add(new Value(DEFAULT_SPINNER_STRING));
        list.addAll(model.alternative);
        adapter.add(list);
        spinner.setAdapter(adapter);
    }
}
